﻿Imports GBDZafiro

Public Class printProducto
    Implements IPrintUtil

    Private Shared anchoTotal As Integer
    Private Shared anchoColumnasImp() As Integer
    Private Shared dtProd As DataTable
    Dim tListado, fecha, Usuario As String
    Dim empresa As Empresa


    Public Sub New(ByRef empresa As Empresa, _
                   Optional ByRef tListado As String = "LISTADO GENERAL DE EXISTENCIAS") 'Que me envie una empresa
        Me.empresa = empresa
        fecha = "FECHA IMPRESION: [" + FormatDateTime(Now, DateFormat.ShortDate) + "]"
        Usuario = Login.usuario.nomUsuario
        Me.tListado = tListado
    End Sub

    Public Function CabeceraImpresion(e As System.Drawing.Printing.PrintPageEventArgs, prFont As System.Drawing.Font, yPos As Single) As Single Implements IPrintUtil.CabeceraImpresion
        ' imprimir la cabecera de la página
        ' se devuelve el valor de la última posición de la impresión
        Dim lineHeight As Single = prFont.GetHeight(e.Graphics)
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim sb As System.Text.StringBuilder
        '
        '
        sb = New System.Text.StringBuilder
        For i As Integer = 0 To dtProd.Columns.Count - 1
            sb.AppendFormat("{0} ", BasePrintUtil.ajustar(dtProd.Columns.Item(i).ColumnName, anchoColumnasImp(i)))
        Next

        yPos += lineHeight
        e.Graphics.DrawString(sb.ToString, prFont, Brushes.Black, 20, yPos)
        '
        sb = New System.Text.StringBuilder
        For i As Integer = 0 To dtProd.Columns.Count - 1
            sb.AppendFormat("{0} ", New String("-"c, anchoColumnasImp(i)))
        Next
        yPos += lineHeight
        e.Graphics.DrawString(sb.ToString, prFont, Brushes.Black, 20, yPos)
        '
        Return yPos
    End Function

    Public Sub ToColumnHeaders(dt As DataTable) Implements IPrintUtil.ToColumnHeaders
        dtProd = dt
        ReDim anchoColumnasImp(dt.Columns.Count - 1)
        anchoColumnasImp(0) = 10
        anchoColumnasImp(1) = 40
        anchoColumnasImp(2) = 10
        anchoColumnasImp(3) = 8
        anchoColumnasImp(4) = 10
    End Sub

    Public Function LineaImpresión(ByRef lineaActual As Integer) As String Implements IPrintUtil.LineaImpresión
        Dim sb As System.Text.StringBuilder
        Dim rProd As DataRow = dtProd.Rows.Item(lineaActual)
        ' tener en cuenta el ancho de cada columna
        sb = New System.Text.StringBuilder

        For i As Integer = 0 To dtProd.Columns.Count - 1
            If i = dtProd.Columns.Count - 1 Then
                sb.AppendFormat("{0} ", BasePrintUtil.ajustar(rProd.Item(i).ToString, anchoColumnasImp(i), True))
            Else
                sb.AppendFormat("{0} ", BasePrintUtil.ajustar(rProd.Item(i).ToString, anchoColumnasImp(i)))
            End If

        Next
        Return sb.ToString
    End Function

    Public Function TituloImpresion(e As System.Drawing.Printing.PrintPageEventArgs, fontTitulo As System.Drawing.Font, yPos As Single) As Single Implements IPrintUtil.TituloImpresion
        Dim lineHeight As Single = fontTitulo.GetHeight(e.Graphics)
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim sb As System.Text.StringBuilder
        '
        '
        Dim displayRectangle _
       As New Rectangle(New Point(20, yPos), New Size(800, yPos))

        Dim format1 As New StringFormat(StringFormatFlags.NoClip)
        ' Set the LineAlignment and Alignment properties for 
        ' both StringFormat objects to different values.
        format1.LineAlignment = StringAlignment.Near
        format1.Alignment = StringAlignment.Center

        e.Graphics.DrawString(empresa.RAZON_SOCIAL, fontTitulo, _
                              Brushes.Black, RectangleF.op_Implicit(displayRectangle), format1) '20. arriba
        yPos += lineHeight

        fontTitulo = New Font("Arial", 15, FontStyle.Bold)
        displayRectangle = New Rectangle(New Point(20, yPos), New Size(800, yPos))
        e.Graphics.DrawString("Nit: " + empresa.NIT, fontTitulo, _
                              Brushes.Black, RectangleF.op_Implicit(displayRectangle), format1) '20. arriba
        yPos += lineHeight

        displayRectangle = New Rectangle(New Point(20, yPos), New Size(800, yPos))
        e.Graphics.DrawString("Dirección: " + empresa.DIRECCION + " " + "Telefono: " + empresa.TELEFONO, _
                              fontTitulo, Brushes.Black, RectangleF.op_Implicit(displayRectangle), format1) '20. arriba
        yPos += lineHeight

        fontTitulo = New Font("Arial", 12, FontStyle.Bold)
        displayRectangle = New Rectangle(New Point(20, yPos), New Size(800, yPos))
        e.Graphics.DrawString(Me.tListado, fontTitulo, _
                              Brushes.Black, RectangleF.op_Implicit(displayRectangle), format1) '20. arriba
        yPos += lineHeight

        fontTitulo = New Font("Arial", 13, FontStyle.Regular)
        displayRectangle = New Rectangle(New Point(20, yPos), New Size(800, yPos))
        e.Graphics.DrawString(fecha + " " + " IMPRESO POR: " + Usuario, fontTitulo, _
                             Brushes.Black, RectangleF.op_Implicit(displayRectangle), format1) '20. arriba
        yPos += lineHeight
        '

        Return yPos
    End Function
End Class
